﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;
using UPC.UbicaTuReu.WEB.Dominio;


namespace UPC.UbicaTuReu.WEB.Controllers
{
    public class SalasController : Controller
    {

        private string oSalaRESTService = "http://localhost:11956/SalasRest.svc/SalasRest";
        JavaScriptSerializer js = new JavaScriptSerializer();
        //
        // GET: /Salas/

        public ActionResult Index()
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(oSalaRESTService);
            req.Method = "GET";
            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            StreamReader reader = new StreamReader(res.GetResponseStream());
            string salaJson = reader.ReadToEnd();
            List<Salas> oSala = js.Deserialize<List<Salas>>(salaJson);
            return View(oSala);
        }

        //
        // GET: /Salas/Details/5

        public ActionResult Details(int id)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(oSalaRESTService + "/" + id);
            req.Method = "GET";
            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            StreamReader reader = new StreamReader(res.GetResponseStream());
            string alumnoJson = reader.ReadToEnd();
            Salas salas = js.Deserialize<Salas>(alumnoJson);
            return View(salas);

        }

        //
        // GET: /Salas/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Salas/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                //string postdata = "{\"SalaID\":3,\"SalaNombre\":\"S\",\"SalaDescripcion\":\"Solo con Lapto\",\"SalaUbicacion\":\"PISO2\"}";
                string postdata = "{\"SalaID\":" + collection["SalaID"] + ",\"SalaNombre\":\"" + collection["SalaNombre"] + "\", \"SalaDescripcion\":\"" + collection["SalaDescripcion"] + "\", \"SalaUbicacion\":\"" + collection["SalaUbicacion"] + "\"}";
                byte[] data = Encoding.UTF8.GetBytes(postdata);
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(oSalaRESTService);
                req.Method = "POST";
                req.ContentLength = data.Length;
                req.ContentType = "application/json";
                var reqStream = req.GetRequestStream();
                reqStream.Write(data, 0, data.Length);
                var res = (HttpWebResponse)req.GetResponse();
                return RedirectToAction("Index");

                
            }
            catch (Exception e)
            {
                ModelState.AddModelError("Error", e.Message);
                return View();
            }
        }
        
        //
        // GET: /Salas/Edit/5
 
        public ActionResult Edit(int id)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(oSalaRESTService + "/" + id);
            req.Method = "GET";
            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            StreamReader reader = new StreamReader(res.GetResponseStream());
            string salaJson = reader.ReadToEnd();
            Salas salas = js.Deserialize<Salas>(salaJson);
            return View(salas);
        }

        //
        // POST: /Salas/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                string postdata = "{\"SalaID\":" + collection["SalaID"] + ",\"SalaNombre\":\"" + collection["SalaNombre"] + "\", \"SalaDescripcion\":\"" + collection["SalaDescripcion"] + "\", \"SalaUbicacion\":\"" + collection["SalaUbicacion"] + "\"}";
                byte[] data = Encoding.UTF8.GetBytes(postdata);
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(oSalaRESTService);
                req.Method = "PUT";
                req.ContentLength = data.Length;
                req.ContentType = "application/json";
                var reqStream = req.GetRequestStream();
                reqStream.Write(data, 0, data.Length);
                var res = (HttpWebResponse)req.GetResponse();
                return RedirectToAction("Index");


                 
                 
            }
            catch (Exception e)
            {
                ModelState.AddModelError("Error", e.Message);
                return View();
            }
        }

        //
        // GET: /Salas/Delete/5
 
        public ActionResult Delete(int id)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(oSalaRESTService + "/" + id);
            req.Method = "GET";
            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            StreamReader reader = new StreamReader(res.GetResponseStream());
            string salaJson = reader.ReadToEnd();
            Salas salas = js.Deserialize<Salas>(salaJson);
            return View(salas);
        }

        //
        // POST: /Salas/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                string postdata = "\"" + id + "\"";
                byte[] data = Encoding.UTF8.GetBytes(postdata);
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(oSalaRESTService);
                req.Method = "DELETE";
                req.ContentLength = data.Length;
                req.ContentType = "application/json";
                var reqStream = req.GetRequestStream();
                reqStream.Write(data, 0, data.Length);
                var res = (HttpWebResponse)req.GetResponse();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
